package com.wst.business.api;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.wst.system.interceptor.encrypt.EncryptDecrypt;
import com.wst.system.interceptor.encrypt.RequestVo;
import com.wst.system.security.IsApI;
import com.wst.system.security.IsSecurity;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestApi {

    @CrossOrigin
    @IsSecurity(isSecurity = false)
    @GetMapping("info4")
    public JSONObject info3() {
        System.out.println("info4");
        return ResultUtil.successInfo("收到信息了");
    }

    @EncryptDecrypt()
    @PostMapping("encrtpy")
    public JSONObject encrtpy(Object obj, HttpServletRequest request, HttpServletResponse response) {
        System.out.println(request.getRequestURI());
        JSONObject object=(JSONObject)obj;
        return ResultUtil.successInfo(object);
    }


    @EncryptDecrypt()
    @PostMapping("/testsave")
    public JSONObject testSave(RequestVo vo){
        System.out.println(vo.toString());
        System.out.println("开始了--->");
        return ResultUtil.successInfo(vo);
    }

    @EncryptDecrypt()
    @PostMapping("/testpageencrtpy")
    public JSONObject testApiPage2(PageSearchVo vo){
        System.out.println(vo.getPageNum());
        //System.out.println(vo.getPageSize());
        //System.out.println("请求结果完整信息:"+JSONObject.toJSONString(vo));
        JSONArray array=new JSONArray();
        for (int i = 0; i < 10 ; i++) {
            JSONObject obj=new JSONObject();
            obj.put("id",1);
            obj.put("input","输入信息");
            obj.put("select","value_02");
            obj.put("radio","value_01");
            JSONArray valueList=new JSONArray();
            valueList.add("value_01");
            valueList.add("value_02");
            obj.put("checkbox",valueList);
            obj.put("textarea","输入的富文本信息");
            obj.put("switch",true);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            obj.put("date",formatter.format(new Date()));
            SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            obj.put("datetime",formatter2.format(new Date()));
            array.add(obj);
        }
        JSONObject data=new JSONObject();
        data.put("count",1000);
        data.put("data",array);
        return ResultUtil.successInfo(data);
    }






    @IsSecurity(isSecurity = false)
    @GetMapping("/info")
    public JSONObject testApiController(){
        return ResultUtil.resultCodeJSON(0,"成功");
    }


    @GetMapping("/info2")
    public JSONObject testApi2Controller(){
        return ResultUtil.resultCodeJSON(0,"这些信息会成功啦");
    }

    @IsApI
    @GetMapping("/info3")
    public JSONObject testApi3Controller(){
        log.info("app 访问");
        return ResultUtil.successInfo("只有app可以访问");
    }

    @GetMapping("/testlabel")
    public JSONObject testApiLabel(){
        JSONArray array=new JSONArray();
        JSONObject obj=new JSONObject();
        obj.put("label","测试label_01");
        obj.put("value","value_01");
        array.add(obj);
        JSONObject obj2=new JSONObject();
        obj2.put("label","测试label_02");
        obj2.put("value","value_02");
        array.add(obj2);
        return ResultUtil.successInfo(array);
    }

    @PostMapping("/testpage")
    public JSONObject testApiPage(@Validated @RequestBody PageSearchVo vo){
        JSONArray array=new JSONArray();
        JSONObject obj=new JSONObject();
        obj.put("id",1);
        obj.put("input","输入信息");
        obj.put("select","value_02");
        obj.put("radio","value_01");

        JSONArray valueList=new JSONArray();
        valueList.add("value_01");
        valueList.add("value_02");
        obj.put("checkbox",valueList);
        obj.put("textarea","输入的富文本信息");
        obj.put("switch",true);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        obj.put("date",formatter.format(new Date()));
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        obj.put("datetime",formatter2.format(new Date()));
        array.add(obj);
        return ResultUtil.successInfoTable(1,array);
    }
}
